package com.travelsmart.client.apresentacao.widget;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;



public class FormFlexTable extends FlexTable {

    private String hoverStyle = null;
    private boolean hasHeader=false;
    public static final String STYLE_TABLE="travelsmart-table";
    public static final String STYLE_HEADER="travelsmart-tableHeader";
    public static final String STYLE_ROW_ODD="travelsmart-grid-odd";
    public static final String STYLE_ROW_EVEN="travelsmart-grid-even";
    public static final String STYLE_ROW_HOVER="travelsmart-grid-hover";
    

    /**
     * Constructor
     * 
     */
    public FormFlexTable(final Widget... headers) {
        super();
        setStyleName(STYLE_TABLE);
        setSize("100%", "100%");
        setHoverStyleName(STYLE_ROW_HOVER);
        setCellSpacing(0);
        sinkEvents(Event.ONMOUSEOVER);
        sinkEvents(Event.ONMOUSEOUT);
        this.headersWidget = headers;
        addHeaders(headers);
    }
    
    public FormFlexTable(final ClickHandler handler, final Widget... headers) {
        this(headers);
        this.addClickHandler(handler);
    }

    private Widget[] headersWidget;
    private void addHeaders(Widget... headersWidget) {
        for (int i = 0; i < headersWidget.length; i++) {
        	this.addHeader(i + 1, headersWidget[i]);
		}
    }
    
    private void addHeader(int column, final Widget headerWidget)    {
        hasHeader=true;
        RowFormatter rowFormatter = getRowFormatter();
        rowFormatter.setStyleName(0, STYLE_HEADER);
        setWidget(0, column, headerWidget);
    }

    

    public void clearGrid() {
    	this.clear();
    	addHeaders(headersWidget);
    }
    
    public void hasHeader(boolean header)   {
        this.hasHeader=header;
    }

   
    public String getId(int row) {
        Label lbl = (Label) this.getWidget(row, 0);
        if (lbl != null)
            return lbl.getText();
        else
            return "0";
    }

   
    public void setId(int row, String id) {
        Label lbl = new Label();
        lbl.setText(id);
        lbl.setVisible(false);
        this.setWidget(row, 0, lbl);

    }

   
    public void setHoverStyleName(String style) {
        hoverStyle = style;
    }

    public String getHoverStyleName() {
        return hoverStyle;
    }

    public void onBrowserEvent(Event event) {
        switch (DOM.eventGetType(event)) {
        case Event.ONMOUSEOVER: {
            // Find out which cell was entered.
            Element td = getEventTargetCell(event);
            if (td == null)
                return;
            Element tr = DOM.getParent(td);
            if (tr == null)
                return;
            Element body = getBodyElement();
            int row = DOM.getChildIndex(body, tr);
            // Fire the event
            mouseEntersRow(this, row);
            break;
        }
        case Event.ONMOUSEOUT: {
            // Find out which cell was exited.
            Element td = getEventTargetCell(event);
            if (td == null)
                return;
            Element tr = DOM.getParent(td);
            if (tr == null)
                return;
            Element body = getBodyElement();
            int row = DOM.getChildIndex(body, tr);
            // Fire the event.
            mouseLeavesRow(this, row);
            break;
        }
        }
        super.onBrowserEvent(event);
    }

    private void mouseEntersRow(Widget sender, int row) {
        if(hasHeader && row==0)
            return;
        if (hoverStyle != null)
            this.getRowFormatter().addStyleName(row, hoverStyle);
    }

    private void mouseLeavesRow(Widget sender, int row) {
        if(hasHeader && row==0)
            return;
        if (hoverStyle != null)
            this.getRowFormatter().removeStyleName(row, hoverStyle);
    }
    /**
     * Adiciona linha na tabela 
     * @param num_linha (numero da linha)
     * @param id (valor capturado evento clique)
     * @param columns (valores das colunas)
     */
    public void addRow(int num_linha, String id, String... columns){
    	if (num_linha + 1 % 2 == 0) {
            this.getRowFormatter().setStyleName(num_linha + 1, FormFlexTable.STYLE_ROW_EVEN);
        } else {
        	this.getRowFormatter().setStyleName(num_linha + 1, FormFlexTable.STYLE_ROW_ODD);
        }
    	this.setId(num_linha + 1, id);
    	for (int j = 0; j < columns.length; j++) {
			String column = columns[j];
			this.setHTML(num_linha + 1, j + 1, column);
		}
    	
    }
    public void addRow(int num_linha, String id, Widget... widgets){
    	if (num_linha + 1 % 2 == 0) {
            this.getRowFormatter().setStyleName(num_linha + 1, FormFlexTable.STYLE_ROW_EVEN);
        } else {
        	this.getRowFormatter().setStyleName(num_linha + 1, FormFlexTable.STYLE_ROW_ODD);
        }
    	this.setId(num_linha + 1, id);
    	for (int j = 0; j < widgets.length; j++) {
    		Widget widget = widgets[j];
			this.setWidget(num_linha + 1, j + 1, widget);
		}
    }

}
